﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;
using VectorPicsEditor.Helpers;
using System.Windows.Media;
using System.Windows.Controls;

namespace VectorPicsEditor.Controls
{
    public partial class LineProperties : CommonProperties
    {
        public LineProperties()
        {
            InitializeComponent();
            this.nameTextBox.LostFocus += this.ShapeNameChanged;
        }

        public override void Initialize(ShapeWrapper shape, System.Windows.Forms.ComboBox comboBox)
        {
            base.Initialize(shape, comboBox);
            this.InitFields();
        }

        private void InitFields()
        {
            base.nameTextBox.Text = base.Shape.Name;
            this.thicknessTextBox.Text = (base.Shape.Shape as Line).StrokeThickness.ToString();
            this.colorPanel.BackColor = Converter.ConvertColor(((this.Shape.Shape as Line).Stroke as SolidColorBrush).Color);
            this.opacityTrackBar.Value = (int)(this.Shape.Shape.Opacity * 100);
            this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
            this.x1TextBox.Text = (this.Shape.Shape as Line).X1.ToString();
            this.y1TextBox.Text = (this.Shape.Shape as Line).Y1.ToString();
            this.x2TextBox.Text = (this.Shape.Shape as Line).X2.ToString();
            this.y2TextBox.Text = (this.Shape.Shape as Line).Y2.ToString();
            this.y2TextBox.TextChanged += new System.EventHandler(this.PositionChanged);
            this.x2TextBox.TextChanged += new System.EventHandler(this.PositionChanged);
            this.y1TextBox.TextChanged += new System.EventHandler(this.PositionChanged);
            this.x1TextBox.TextChanged += new System.EventHandler(this.PositionChanged);
        }

        void ShapeNameChanged(object sender, EventArgs e)
        {
            if (this.Shape.Name != nameTextBox.Text)
            {
                this.Shape.Name = nameTextBox.Text;
                this.Shape.WasChanged = true;
                int index = this.ShapesComboBox.SelectedIndex;
                this.ShapesComboBox.Items[index] = this.Shape.Name;
            }
        }

        private void PositionChanged(object sender, EventArgs e)
        {
            try
            {
                (this.Shape.Shape as Line).X1 = double.Parse(x1TextBox.Text);
                (this.Shape.Shape as Line).Y1 = double.Parse(y1TextBox.Text);
                (this.Shape.Shape as Line).X2 = double.Parse(x2TextBox.Text);
                (this.Shape.Shape as Line).Y2 = double.Parse(y2TextBox.Text);
            }
            catch(FormatException)
            {
                //Ignore this
            }
            finally
            {
                this.Shape.WasChanged = true;
                this.Shape.UnselectLine();
            }
            
        }

        private void ChangeColor(object sender, EventArgs e)
        {
            var newColor = base.OpenColorDialog();
            if (newColor != null)
            {
                this.colorPanel.BackgroundImage = null;
                this.colorPanel.BackColor = newColor.Value;
                (this.Shape.Shape as Line).Stroke = new SolidColorBrush(Converter.ConvertColor(newColor.Value));
                this.Shape.WasChanged = true;
            }
        }

        private void ChangeThickness(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.thicknessTextBox.Text);
                (this.Shape.Shape as Line).StrokeThickness = x;
            }
            catch (FormatException)
            {
                //Ignore this
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void ChangeOpacity(object sender, EventArgs e)
        {
            this.Shape.Shape.Opacity = this.opacityTrackBar.Value / 100f;
            this.Shape.WasChanged = true;
        }

        private void ChangeZindex(object sender, EventArgs e)
        {
            try
            {
                int c = int.Parse(this.zindexTextBox.Text);
                Canvas.SetZIndex(this.Shape.Shape, c);
            }
            catch (FormatException)
            {
                this.zindexTextBox.Text = Canvas.GetZIndex(this.Shape.Shape).ToString();
            }
            finally
            {
                this.Shape.WasChanged = true;
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            //TODO: remove all events related to this shape
            this.Shape.Remove(this.Shape.Shape);
            int index = this.ShapesComboBox.SelectedIndex;
            if (index != 0)
                this.ShapesComboBox.SelectedIndex = index - 1;
            else
                this.Dispose();
            this.ShapesComboBox.Items.RemoveAt(index);
        }
    }
}
